package com.ktjiaoyu.news.dao.impl;

import com.ktjiaoyu.news.dao.BaseDao;
import com.ktjiaoyu.news.dao.PatientDao;
import com.ktjiaoyu.news.entity.NewsAdmin;
import com.ktjiaoyu.news.entity.Patient;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.zip.CheckedOutputStream;

public class PatientDaoImpl extends BaseDao implements PatientDao {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    @Override
    public int count() {
        int count = 0;
        try {
            conn = getConnection();
            String sql = "select count(*) from news_user";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, stmt, rs);
        }
        return count;
    }

    @Override
    public List<Patient> getPage(Patient patient, Integer pageNo, Integer pageSize) {
        List<Patient> list = new ArrayList<>();
        try {
            conn = getConnection();
            List<Object> params = new ArrayList<>();
            String sql = "select * from news_user where 1=1 ";
            if (patient != null) {
                if (patient.getPatientName() != null && !(patient.getPatientName().equals(""))) {
                    sql += " and patientName like ?";
                    params.add(patient.getPatientName());
                }
                if (patient.getPhoneNum() != null && !(patient.getPhoneNum().equals(""))) {
                    sql += " and phoneNum like ? ";
                    params.add(patient.getPhoneNum());
                }
            }
            // sql += " order by xxx , xxx desc"

            sql += " limit ?, ?";
            params.add((pageNo - 1) * pageSize);
            params.add(pageSize);
            stmt = conn.prepareStatement(sql);
            int i = 0;
            if (patient.getPatientName() != null && !(patient.getPatientName().equals(""))) {
                stmt.setObject(i + 1, "%" + params.get(i).toString() + "%");
                i++;
            }
            if (patient.getPhoneNum() != null && !(patient.getPhoneNum().equals(""))) {
                stmt.setObject(i + 1, "%" + params.get(i).toString() + "%");
                i++;
            }
            for (; i < params.size(); i++) {
                stmt.setObject(i + 1, params.get(i));
            }
            rs = stmt.executeQuery();
            while (rs.next()) {
                Patient newPatient = new Patient();
                newPatient.setId(rs.getInt("patientID"));
                newPatient.setPatientName(rs.getString("patientName"));
                newPatient.setPhoneNum(rs.getString("phoneNum"));
                newPatient.setBirthDate(rs.getString("birthDate"));
                newPatient.setGender(rs.getString("gender"));
                newPatient.setEmail(rs.getString("email"));
                newPatient.setIdentityNum(rs.getString("identityNum"));
                newPatient.setAddress(rs.getString("address"));
                list.add(newPatient);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, stmt, rs);
        }
        return list;
    }
}
